package com.cn.web.controller.Equipment;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.EquipmentoperationDto;
import com.cn.bean.dto.SingleRegistrationTableDto;
import com.cn.bean.pojo.PartsManagement.EquipmentBasicInfoTable;
import com.cn.bean.pojo.PartsManagement.EquipmentDisassemblySubTable;
import com.cn.bean.pojo.PartsManagement.EquipmentInstallationMainTable;
import com.cn.bean.pojo.PartsManagement.EquipmentInstallationSubTable;
import com.cn.bean.pojo.lbj.*;
import com.cn.bean.vo.Equipment.ComponentBasicInformation;
import com.cn.config.config.Result;
import com.cn.service.server.Equipment.EquipmentoperationService;
import com.cn.service.server.Equipment.WorkOrderPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class EquipmentoperationController {
    @Autowired
    private EquipmentoperationService equipmentoperationService;
    @Autowired
    private WorkOrderPlanService workOrderPlanService;
    //查询主表
    @RequestMapping("/select/EquipmentInstallationMainTable")
    @ResponseBody
    public List<EquipmentInstallationMainTable> selectMain(@RequestBody EquipmentInstallationMainTable equipmentInstallationMainTable) {
        List<EquipmentInstallationMainTable> assists = equipmentoperationService.selectEquipmentoperation(equipmentInstallationMainTable);
        return assists;
    }
    //查询设备基础信息表
    @RequestMapping("/select/EquipmentBasicInfoTable")
    @ResponseBody
    public List<EquipmentBasicInfoTable> selectEquipmentBasicInfoTable(@RequestBody EquipmentBasicInfoTable equipmentBasicInfoTable) {
        List<EquipmentBasicInfoTable> assists = equipmentoperationService.selectEquipmentBasicInfoTable(equipmentBasicInfoTable);
        return assists;
    }
    //查询零件基础信息表
    @RequestMapping("/select/componentBasicInformation")
    @ResponseBody
    public List<ComponentBasicInformationTable> selectComponentBasicInformation(@RequestBody ComponentBasicInformationTable componentBasicInformation) {
        List<ComponentBasicInformationTable> assists = equipmentoperationService.selectComponentBasicInformation(componentBasicInformation);
        return assists;
    }
    //根据前端发来的操作类别不同，查询到的数据也不同
    @RequestMapping("/select/selectEquipmentSub")
    @ResponseBody
    public List<EquipmentoperationDto> selectEquipmentSub(@RequestBody EquipmentoperationDto equipmentoperationDto) {
        int a=equipmentoperationDto.getOperationCategory();
        int id=equipmentoperationDto.getId();
        if (a==1){
            List<EquipmentoperationDto> assists = equipmentoperationService.selectEquipmentInstallationSubTable(id);
            return assists;
        }
            List<EquipmentoperationDto> assists = equipmentoperationService.selectEquipmentDisassemblySubTable(id);
            return assists;
    }
    //保存按钮
    @RequestMapping("/insert/EquipmentSub")
    @ResponseBody
    public Result insertEquipmentSub(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
        EquipmentInstallationMainTable mainTable = new EquipmentInstallationMainTable();
        Integer id = equipmentoperationService.getMainAutoid();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        String userName = jsonObject.getString("userName");
        int workOrderNumber=jsonObject.getInteger("workOrderNumber");
        String equipmentCode=jsonObject.getString("equipmentCode");
        String equipmentName=jsonObject.getString("equipmentName");
        int operationCategory=jsonObject.getInteger("operationCategory");
        int equipmentId=jsonObject.getInteger("equipmentId");
        mainTable.setId(id);
        mainTable.setDjNumber(id);
        mainTable.setEquipmentId(equipmentId);
        mainTable.setEquipmentCode(equipmentCode);
        mainTable.setEquipmentName(equipmentName);
        mainTable.setWorkOrderNumber(String.valueOf(workOrderNumber));
        mainTable.setOperationCategory(operationCategory);
        mainTable.setDjStatus(0);
        mainTable.setDjTime(format.format(date));
        mainTable.setCjr(userName);
        mainTable.setCjsj(format.format(date));
        mainTable.setDjNumber(id);
        mainTable.setIsdel(0);
        int a = equipmentoperationService.insertEquipmentInstallationMain(mainTable);

        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<EquipmentoperationDto> djBhList = dataArray.toJavaList(EquipmentoperationDto.class);
            if (operationCategory == 1) {
                for (EquipmentoperationDto dataStructureDto : djBhList) {
                    EquipmentInstallationSubTable subTable = new EquipmentInstallationSubTable();
                    EquipmentCurrentComponentSingleCompositionTable compositionTable=new EquipmentCurrentComponentSingleCompositionTable();
                    ComponentSingleRegistrationTable componentSingle=new ComponentSingleRegistrationTable();
                    BeanUtils.copyProperties(dataStructureDto, subTable);
                    Integer autoid = equipmentoperationService.getEquipmentInstallationSubAutoid();
                    autoid = autoid != null ? ++autoid : 1;
                    subTable.setId(autoid);
                    subTable.setMid(id);
                    compositionTable.setId(autoid);
                    compositionTable.setM_1_ID(subTable.getEquipmentStructureId());
                    compositionTable.setComponentId(subTable.getComponentId());
                    compositionTable.setUniqueCode(subTable.getUniqueCode());
                    compositionTable.setQuantity(subTable.getQuantity());
                    compositionTable.setSingleStatus(subTable.getSingleStatus());
                    componentSingle.setUniqueCode(subTable.getUniqueCode());
                    componentSingle.setCirculationState(2);
                    int z=equipmentoperationService.updateComponentStatus(componentSingle);
                    int b = equipmentoperationService.insertEquipmentInstallationSub(subTable);
                    int x=equipmentoperationService.insertCurrentComponent(compositionTable);
                    int t=workOrderPlanService.openWorkOrder(subTable.getUniqueCode());
                    if (b != 1&&x != 1&&z !=1&&t!=1) {
                        return Result.failure("500");
                    }
                }
            }
            if (operationCategory == 2) {
                for (EquipmentoperationDto dataStructureDto : djBhList) {
                    EquipmentDisassemblySubTable subTable = new EquipmentDisassemblySubTable();
                    EquipmentCurrentComponentSingleCompositionTable compositionTable=new EquipmentCurrentComponentSingleCompositionTable();
                    ComponentSingleRegistrationTable componentSingle=new ComponentSingleRegistrationTable();
                    BeanUtils.copyProperties(dataStructureDto, subTable);
                    Integer autoid = equipmentoperationService.getEquipmentDisassemblyAutoid();
                    autoid = autoid != null ? ++autoid : 1;
                    subTable.setId(autoid);
                    subTable.setMid(id);
                    compositionTable.setM_1_ID(subTable.getEquipmentStructureId());
                    compositionTable.setUniqueCode(subTable.getUniqueCode());
                    componentSingle.setUniqueCode(subTable.getUniqueCode());
                    componentSingle.setCirculationState(3);
                    List<EquipmentCurrentComponentSingleCompositionTable>tableList=equipmentoperationService.selectEquipment(subTable.getUniqueCode());
                    if (tableList.size()==0) {
                        return Result.failure("501");
                    }
                    int z=equipmentoperationService.updateComponentStatus(componentSingle);
                    int c = equipmentoperationService.insertEquipmentDisassemblySubTable(subTable);
                    int d=equipmentoperationService.delCurrentComponent(compositionTable);
                    int t=workOrderPlanService.openWorkOrder(subTable.getUniqueCode());
                    if (z!=1&&c != 1&&d != 1&&t!=1) {
                        return Result.failure("500");
                    }
                }
            }
            if (operationCategory == 5) {
                for (EquipmentoperationDto dataStructureDto : djBhList) {
                    EquipmentDisassemblySubTable disassemblySubTable = new EquipmentDisassemblySubTable();
                    EquipmentInstallationSubTable installationSubTab = new EquipmentInstallationSubTable();
                    EquipmentCurrentComponentSingleCompositionTable compositionTable=new EquipmentCurrentComponentSingleCompositionTable();
                    ComponentSingleRegistrationTable componentSingle=new ComponentSingleRegistrationTable();
                    BeanUtils.copyProperties(dataStructureDto, installationSubTab);
                    BeanUtils.copyProperties(dataStructureDto, disassemblySubTable);
                    Integer autoid = equipmentoperationService.getEquipmentDisassemblyAutoid();
                    autoid = autoid != null ? ++autoid : 1;
                    installationSubTab.setId(autoid);
                    installationSubTab.setMid(id);
                    disassemblySubTable.setId(autoid);
                    disassemblySubTable.setMid(id);
                    compositionTable.setId(autoid);
                    compositionTable.setM_1_ID(installationSubTab.getEquipmentStructureId());
                    compositionTable.setUniqueCode(installationSubTab.getUniqueCode());
                    compositionTable.setSingleStatus(installationSubTab.getSingleStatus());
                    compositionTable.setQuantity(installationSubTab.getQuantity());
                    componentSingle.setUniqueCode(installationSubTab.getUniqueCode());
                    componentSingle.setCirculationState(3);
                    int d=equipmentoperationService.delCurrentComponent(compositionTable);
                    int z=equipmentoperationService.updateComponentStatus(componentSingle);
                    int c = equipmentoperationService.insertEquipmentDisassemblySubTable(disassemblySubTable);
                    int t=workOrderPlanService.openWorkOrder(disassemblySubTable.getUniqueCode());
                    componentSingle.setCirculationState(2);
                    int v=equipmentoperationService.updateComponentStatus(componentSingle);
                    int b = equipmentoperationService.insertEquipmentInstallationSub(installationSubTab);
                    int x=equipmentoperationService.insertCurrentComponent(compositionTable);
                    int y=workOrderPlanService.openWorkOrder(installationSubTab.getUniqueCode());
                    if (c != 1&&d != 1&&z!=1&&t!=1&&v!=1&&b!=1&&x!=1&&y!=1) {
                        return Result.failure("500");
                    }
                }
            }

        }
        return Result.success(200);
    }
    //修改按钮
//    @RequestMapping("/update/EquipmentSub")
//    @ResponseBody
//    public Result updateEquipmentSub(@RequestBody JSONObject jsonObject) {
//        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//
//        EquipmentInstallationMainTable mainTable = new EquipmentInstallationMainTable();
//
//        Date date=new Date();
//        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        int id = jsonObject.getInteger("id");
//        String userName = jsonObject.getString("userName");
//        int equipmentId=jsonObject.getInteger("equipmentId");
//        String equipmentCode=jsonObject.getString("equipmentCode");
//        String equipmentName=jsonObject.getString("equipmentName");
//        int operationCategory=jsonObject.getInteger("operationCategory");
//        int equipmentStatus=jsonObject.getInteger("equipmentStatus");
//        mainTable.setId(id);
//        mainTable.setEquipmentId(equipmentId);
//        mainTable.setEquipmentCode(equipmentCode);
//        mainTable.setEquipmentName(equipmentName);
//        mainTable.setOperationCategory(operationCategory);
//        mainTable.setEquipmentStatus(equipmentStatus);
//        mainTable.setCjr(userName);
//        mainTable.setCjsj(format.format(date));
//        int a = equipmentoperationService.updateEquipmentInstallationMain(mainTable);
//        if (a != 1) {
//            return Result.failure("500","更新主表失败");
//        }
//        List<EquipmentoperationDto> equipmentOperation=equipmentoperationService.selectInstallationSubTable(id);
//        List<EquipmentoperationDto>equipmentoperationDtos=equipmentoperationService.selectDisassemblySubTable(id);
//        if (equipmentOperation.size()>0||equipmentoperationDtos.size()>0){
//            int sub = equipmentoperationService.delEquipmentInstallationSub(id);
//            int sub2 = equipmentoperationService.delEquipmentDisassemblySub(id);
//        }
//        if (dataArray != null && !dataArray.isEmpty()) {
//            List<EquipmentoperationDto> djBhList = dataArray.toJavaList(EquipmentoperationDto.class);
//            if (operationCategory == 1) {
//                for (EquipmentoperationDto dataStructureDto : djBhList) {
//                    EquipmentInstallationSubTable subTable = new EquipmentInstallationSubTable();
//                    BeanUtils.copyProperties(dataStructureDto, subTable);
//                    Integer autoid = equipmentoperationService.getEquipmentInstallationSubAutoid();
//                    autoid = autoid != null ? ++autoid : 1;
//                    subTable.setId(autoid);
//                    subTable.setMid(id);
//                    int b = equipmentoperationService.insertEquipmentInstallationSub(subTable);
//                    if (b != 1) {
//                        return Result.failure("500");
//                    }
//                }
//            }
//            if (operationCategory==2){
//                for (EquipmentoperationDto dataStructureDto : djBhList) {
//                    EquipmentDisassemblySubTable subTable = new EquipmentDisassemblySubTable();
//                    BeanUtils.copyProperties(dataStructureDto, subTable);
//                    Integer autoid = equipmentoperationService.getEquipmentDisassemblyAutoid();
//                    autoid = autoid != null ? ++autoid : 1;
//                    subTable.setId(autoid);
//                    subTable.setMid(id);
//                    int c = equipmentoperationService.insertEquipmentDisassemblySubTable(subTable);
//                    if (c != 1) {
//                        return Result.failure("500");
//                    }
//                }
//            }
//
//        }
//        return Result.success(200);
//    }
    //审核按钮
    @RequestMapping("/update/updateAudit")
    @ResponseBody
    public Result updateAudit(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        JSONArray workArray = jsonObject.getJSONArray("workOrderNumber");
        String userName = jsonObject.getString("userName");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (djBhArray != null && !djBhArray.isEmpty()&&workArray != null && !workArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            List<Integer> workList = workArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                equipmentoperationService.updateAudit(id);
            }
            for (Integer workOrderNumber : workList) {
                equipmentoperationService.completeWorkOrde(workOrderNumber);
            }
            return Result.success(200);
        }
        return Result.failure("500", "审核失败");
    }
    //删除按钮，只改状态，不删数据
    @RequestMapping("/del/EquipmentSub")
    @ResponseBody
    public Result delEquipmentSub(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        JSONArray dataArray = jsonObject.getJSONArray("operationCategory");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            List<Integer> dataList = dataArray.toJavaList(Integer.class);
            for (Integer operationCategory : dataList) {
                if (operationCategory==1){
                    for (Integer id : djBhList) {
                        equipmentoperationService.delMain(id);
                        equipmentoperationService.delSubTable(id);
                    }
                }
                if (operationCategory==2){
                    for (Integer id : djBhList) {
                        equipmentoperationService.delMain(id);
                        equipmentoperationService.delDisassemblySubTable(id);
                    }
                }

            }
            return Result.success(200);
        }
        return Result.failure("500", "删除失败");
    }
}
